<?php

App::uses('AdminAppController', 'Admin.Controller');
App::uses('AppController', 'Controller');

class CompaniesController extends AdminAppController {

    var $helpers = array('Form');

    function index() {
        $this->paginate = array(
            //'conditions' => array('Admin.id !=' => '6'),
            'limit' => 25,
            'order' => array('Company.id' => 'desc')
        );

        // we are using the 'Admin' model
        $rows = $this->paginate('Company');

        // pass the value to our view.ctp
        $this->set('rows', $rows);
    }

    function edit($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }
        $post = $this->Company->findById($id);
        if (!$post) {
            throw new NotFoundException(__('Invalid post'));
        }

        if ($this->request->is(array('post', 'put'))) {
            $this->Company->id = $id;            

            if ($this->Company->save($this->request->data)) {
                $this->Session->setFlash('Company has been updated.!', 'default', array(), 'good');
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash('Unable to update company.!', 'default', array(), 'bad');
        }

        if (!$this->request->data) {
            $this->request->data = $post;
        }
    }

    function add() {

        if ($this->request->is(array('post', 'put'))) {
            $this->request->data['date_added'] = date("Y-m-d H:i:s");

            if ($this->Company->save($this->request->data)) {
                $this->Session->setFlash('Company has been created.!', 'default', array(), 'good');
                return $this->redirect(array('action' => 'index'));
            }
            $this->Session->setFlash('Unable to create company.!', 'default', array(), 'bad');
        }
    }

    function delete($id = null) {
        if (!$id) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('get')) {
            throw new MethodNotAllowedException();
        }


        if ($this->Company->delete($id)) {
            $this->Session->setFlash('Delete success!', 'default', array(), 'good');
        } else {
            $this->Session->setFlash('Delete unsuccess!', 'default', array(), 'bad');
        }
        $this->Redirect(array('action' => 'index'));
    }

}
